﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class FTradeCatheringDAL
    {
        /// <summary>
        /// 添加新的　收款记录
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(FTradeCatheringInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_Add", obj.FCTitle, obj.ShoukuanRiQi, obj.JingXiaoShang, obj.JinE, obj.KuanXiangLeiXing, obj.RealName, obj.IsAdjustable, obj.State, obj.Remark, obj.SourceTag, obj.MoneyFrom, obj.oldKuanXiangLeiXing);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        /// <summary>
        /// 删除　收款记录 如果收款记录已冲过帐,则不能删除
        /// </summary>
        /// <param name="FCatheringId"></param>
        /// <returns></returns>
        public static string Delete(int FCatheringId, string remark, decimal DongJieJinE)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_Delete", FCatheringId, DongJieJinE,remark);
            try
            {
                return DB.ExecuteScalar(dbCommand).ToString();
            }
            catch(Exception exc)
            {
                return exc.Message;
            }
        }
        /// <summary>
        /// 删除　如果收款记录未冲过帐,则能删除
        /// </summary>
        /// <param name="FCatheringId"></param>
        /// <returns></returns>
        public static string DeleteAll(int FCatheringId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_DeleteAll", FCatheringId);
            try
            {
                return DB.ExecuteScalar(dbCommand).ToString();
            }
            catch (Exception exc)
            {
                return exc.Message;
            }
        }

        public static IDataReader GetById(int FCatheringId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_GetById", FCatheringId);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 根据经销商及状态获取其下可用的所有收款记录 用于下拉列表
        /// </summary>
        /// <param name="JingXiaoShang"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public static IDataReader GetByJingXiaoShang(string JingXiaoShang, string State)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_GetByJingXiaoShang", JingXiaoShang, State);
            return DB.ExecuteReader(dbCommand);
        }

        public static DataTableCollection GetPagination(string WhereSql, string WhereSql2, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql1", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "WhereSql2", DbType.String, WhereSql2);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static DataTableCollection GetHistoryPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_GetHistoryPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        /// <summary>
        /// 修改　收款记录基本信息如果剩余金额不等于收款金额,则只能改备注信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool Update(FTradeCatheringInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_Update", obj.FCatheringId, obj.FCTitle, obj.ShoukuanRiQi, obj.JingXiaoShang, obj.JinE, obj.KuanXiangLeiXing, obj.Remark, obj.MoneyFrom, obj.oldKuanXiangLeiXing);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 获取每笔收款的最终充值去向 与融资资金走向说明报表 共用
        /// </summary>
        /// <param name="FCatheringId"></param>
        /// <returns></returns>
        public static DataTableCollection GetLastChonZhi(int FCatheringId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_GetLastChonZhi", FCatheringId);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
        /// <summary>
        /// 获取调帐(系统) 的调帐来源记录
        /// </summary>
        /// <param name="FCatheringId"></param>
        /// <returns></returns>
        public static DataTable GetAdjustingMoneyList(int FCatheringId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCatheringHistory_GetAdjustingMoneyList", FCatheringId);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        /// <summary>
        ///经销商回款明细表
        /// </summary>
        /// <param name="FCatheringId"></param>
        /// <returns></returns>
        public static DataTable GetHuKunAgentList(string agentsqltemp,string Fdata, string Tdate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_GetHuKunAgentList",agentsqltemp, Fdata, Tdate);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        /// <summary>
        /// 获取经销商回款 金额
        /// </summary>
        /// <param name="sqlwhere"></param>
        /// <returns></returns>
        public static object GetHuKunJinE(string sqlwhere)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeCathering_GetHuKunJinE", sqlwhere);
            return DB.ExecuteScalar(dbCommand);
        }

    }
}
